<?php
/**
 * Created by PhpStorm.
 * User: *!N.J
 * Date: 2021/9/2
 * Time: 9:02
 * QQ:1467572213
 */

namespace app\agent\controller;

use think\facade\Request;
use think\facade\Session;
use think\Db;
use \app\agent\model\User as User_m;

class User extends Base
{
    public function index()
    {
        $agent_id = Session::get('agent_id');
        $keyword = Request::get('keyword');
        $level_info = User_m::level_info();
        $this->assign('level_info',$level_info);
        $list = User_m::index($agent_id,$keyword);
        // $page = $list->render();
        // $this->assign('pages', $page);
        $this->assign('list', $list);
        User_m::day_order();
        return $this->fetch();
    }
    
    

    public function edit($id)
    {
        $agent_id = Session::get('agent_id');
        if (!$id) $this->error('参数错误');
        $level_info = User_m::level_info();
        $this->assign('level_info',$level_info);
        $user_info = User_m::user_info($agent_id,$id);
        $this->assign('user_info',$user_info);
        if (Request::isPost()){
            $id = Request::post('id');
            $info['level'] = Request::post('level');
            $info['update_time'] = time();
            $edit = User_m::edit($agent_id,$id,$info);
            switch ($edit)
            {
                case 'success':
                    $this->success('修改成功');
                    break;
                case 'error':
                    $this->error('修改失败');
                    break;
            }
        }
        return $this->fetch();
    }

    public function level($id)
    {
        $agent_id = Session::get('agent_id');
        if (!$id) $this->error('参数错误');
        $level_info = User_m::level_info();
        $this->assign('level_info',$level_info);
        $user_info = User_m::user_info($agent_id,$id);
        $this->assign('user_info',$user_info);
        if (Request::isPost()){
            $id = Request::post('id');
            $info['level'] = Request::post('level');
            $info['update_time'] = time();
            $edit = User_m::edit($agent_id,$id,$info);
            switch ($edit)
            {
                case 'success':
                    $this->success('修改成功');
                    break;
                case 'error':
                    $this->error('修改失败');
                    break;
            }
        }
        return $this->fetch();
    }

    public function group($id)
    {
        $agent_id = Session::get('agent_id');
        if (!$id) $this->error('参数错误');
        $level_info = User_m::level_info();
        $this->assign('level_info',$level_info);
        $user_info = User_m::user_info($agent_id,$id);
        $this->assign('user_info',$user_info);
        $group_info = User_m::get_group_info();
        $this->assign('group_info',$group_info);
        if (Request::isPost()){
            $uid = Request::post('uid');
            $group_id = Request::post('group_id');
            $set_group = User_m::set_group($uid,$group_id);
            switch ($set_group)
            {
                case 'success':
                    $this->success('修改分组成功');
                    break;
                case 'error':
                    $this->error('修改分组失败');
                    break;
            }
        }
        return $this->fetch();
    }
    
    public function dan()
    {
        return $this->fetch();
    }

}